//
//  ChangePwdViewController.m
//  YTwallet
//
//  Created by ZhangLin on 2019/3/18.
//  Copyright © 2019年 hadrlog. All rights reserved.
//

#import "ChangePwdViewController.h"
#import "ChangePwdTableViewCell.h"
#import <Masonry.h>
#import "ChangePwdModelPack.h"

@interface ChangePwdViewController ()<UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate>

@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)NSMutableArray *tipsArray;
@property(nonatomic,strong)NSMutableArray *tipsValueArray;
@property(nonatomic,strong)UIButton *nextStepBtn;

@end

@implementation ChangePwdViewController

-(NSMutableArray *)tipsArray
{
    if (_tipsArray == nil) {
        _tipsArray = [NSMutableArray array];
    }
    return _tipsArray;
}

-(NSMutableArray *)tipsValueArray
{
    if (_tipsValueArray == nil) {
        _tipsValueArray = [NSMutableArray array];
    }
    return _tipsValueArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"修改密码";
    self.view.backgroundColor = [UIColor colorWithRed:242 / 255.0 green:242 / 255.0 blue:242 / 255.0 alpha:1];
    //创建TableView
    [self createTableView];
    //保存新密码
    [self saveNewPwd];
}
-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    //输入框都有值，btn可点击
    [self check];
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}



-(void)check
{
    NSIndexPath *indexPath0 = [NSIndexPath indexPathForRow:0 inSection:0];
    ChangePwdTableViewCell *cell0 = (ChangePwdTableViewCell *)[self.tableView cellForRowAtIndexPath:indexPath0];
    
    NSIndexPath *indexPath1 = [NSIndexPath indexPathForRow:1 inSection:0];
    ChangePwdTableViewCell *cell1 = (ChangePwdTableViewCell *)[self.tableView cellForRowAtIndexPath:indexPath1];
    
    NSIndexPath *indexPath2 = [NSIndexPath indexPathForRow:2 inSection:0];
    ChangePwdTableViewCell *cell2 = (ChangePwdTableViewCell *)[self.tableView cellForRowAtIndexPath:indexPath2];
    
    NSIndexPath *indexPath3 = [NSIndexPath indexPathForRow:3 inSection:0];
    ChangePwdTableViewCell *cell3 = (ChangePwdTableViewCell *)[self.tableView cellForRowAtIndexPath:indexPath3];
    
    [cell0.tipsValueTf addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    [cell1.tipsValueTf addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    [cell2.tipsValueTf addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    [cell3.tipsValueTf addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
}
-(void)textFieldDidChange :(UITextField *)textField
{
    NSIndexPath *indexPath0 = [NSIndexPath indexPathForRow:0 inSection:0];
    ChangePwdTableViewCell *cell0 = (ChangePwdTableViewCell *)[self.tableView cellForRowAtIndexPath:indexPath0];
    
    NSIndexPath *indexPath1 = [NSIndexPath indexPathForRow:1 inSection:0];
    ChangePwdTableViewCell *cell1 = (ChangePwdTableViewCell *)[self.tableView cellForRowAtIndexPath:indexPath1];
    
    NSIndexPath *indexPath2 = [NSIndexPath indexPathForRow:2 inSection:0];
    ChangePwdTableViewCell *cell2 = (ChangePwdTableViewCell *)[self.tableView cellForRowAtIndexPath:indexPath2];
    
    NSIndexPath *indexPath3 = [NSIndexPath indexPathForRow:3 inSection:0];
    ChangePwdTableViewCell *cell3 = (ChangePwdTableViewCell *)[self.tableView cellForRowAtIndexPath:indexPath3];
    
    if (cell0.tipsValueTf.text.length > 0 &&
        cell1.tipsValueTf.text.length > 0 &&
        cell2.tipsValueTf.text.length > 0 &&
        cell3.tipsValueTf.text.length > 0) {
        self.nextStepBtn.userInteractionEnabled = YES;
        self.nextStepBtn.backgroundColor = ETCONFIRMCOLOR;
    }else
    {
        self.nextStepBtn.userInteractionEnabled = NO;
        self.nextStepBtn.backgroundColor = ETCOMMONCOLOR;
    }
}

-(void)saveNewPwd
{
    
    self.nextStepBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [self.nextStepBtn setTitle:@"保存新密码" forState:(UIControlStateNormal)];
    self.nextStepBtn.userInteractionEnabled = NO;
    [self.nextStepBtn setTitleColor:[UIColor whiteColor]forState:(UIControlStateNormal)];
    self.nextStepBtn.titleLabel.font = [UIFont systemFontOfSize:16.0];
    self.nextStepBtn.backgroundColor = [UIColor colorWithRed:219/255.0 green:224/255.0 blue:255/255.0 alpha:1.0];
    self.nextStepBtn.layer.cornerRadius = 22;
    [self.view addSubview:self.nextStepBtn];
    [self.nextStepBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.tableView.mas_bottom).offset(40);
        make.left.mas_equalTo(25);
        make.right.mas_equalTo(-25);
        make.height.mas_equalTo(44);
    }];
    
    [self.nextStepBtn addTarget:self action:@selector(nextStepBtnAction) forControlEvents:(UIControlEventTouchUpInside)];
}
-(void)nextStepBtnAction
{
    NSLog(@"保存新密码");
    NSIndexPath *indexPath0 = [NSIndexPath indexPathForRow:0 inSection:0];
    ChangePwdTableViewCell *cell0 = (ChangePwdTableViewCell *)[self.tableView cellForRowAtIndexPath:indexPath0];
    
    NSIndexPath *indexPath1 = [NSIndexPath indexPathForRow:1 inSection:0];
    ChangePwdTableViewCell *cell1 = (ChangePwdTableViewCell *)[self.tableView cellForRowAtIndexPath:indexPath1];
    
    NSIndexPath *indexPath2 = [NSIndexPath indexPathForRow:2 inSection:0];
    ChangePwdTableViewCell *cell2 = (ChangePwdTableViewCell *)[self.tableView cellForRowAtIndexPath:indexPath2];
    
    NSIndexPath *indexPath3 = [NSIndexPath indexPathForRow:3 inSection:0];
    ChangePwdTableViewCell *cell3 = (ChangePwdTableViewCell *)[self.tableView cellForRowAtIndexPath:indexPath3];
    
    if (cell0.tipsValueTf.text.length == 0) {
        [self showFailedResultTips:@"账号不能为空" animated:YES forSeconds:FAIL_SHOW_TIME_DEF];
        return;
    }
    if (cell1.tipsValueTf.text.length == 0) {
        [self showFailedResultTips:@"原密码不能为空" animated:YES forSeconds:FAIL_SHOW_TIME_DEF];
        return;
    }
    if (cell2.tipsValueTf.text.length == 0) {
        [self showFailedResultTips:@"新密码不能为空" animated:YES forSeconds:FAIL_SHOW_TIME_DEF];
        return;
    }
    if (cell3.tipsValueTf.text.length == 0) {
        [self showFailedResultTips:@"新密码不能为空" animated:YES forSeconds:FAIL_SHOW_TIME_DEF];
        return;
    }
    
    if (![cell2.tipsValueTf.text isEqualToString:cell3.tipsValueTf.text]) {
        [self showFailedResultTips:@"两次密码不一致" animated:YES forSeconds:FAIL_SHOW_TIME_DEF];
        return;
    }
    
    if (![AppTools checkPassword:cell2.tipsValueTf.text] || ![AppTools checkPassword:cell3.tipsValueTf.text]) {
        [self showFailedResultTips:@"密码格式错误" animated:YES forSeconds:FAIL_SHOW_TIME_DEF];
        return;
    }
    
    [self.view endEditing:YES];
    
    
    ChangePwdRequest *request = [[ChangePwdRequest alloc] init];
    request.newPassWord = [cell2.tipsValueTf.text md5];
    request.confirmPassWord = [cell3.tipsValueTf.text md5];
    request.oldPassWord = [cell1.tipsValueTf.text md5];
    NSDictionary *dic = [request mj_keyValues];
    NSString *url = [[ServerAddressModel shareInstance] fullTAnd0AddrWithParameter:@"/epay_apps/password/updatePwdA0500"];
    [[ETONEHttpClient sharedInstance] doHTTPSPOST:url Parameters:dic HttpBody:nil SuccessHandler:^(id responseObject) {
        
        NSDictionary *dic = [responseObject mj_JSONObject];
        NSString *code = dic[@"code"];
        if ([@"0000" isEqualToString:code]) {
            
            dispatch_async(dispatch_get_main_queue(), ^{
                [self showSuccessResultTips:@"修改成功" animated:YES forSeconds:SUCCESS_SHOW_TIME_DEF completionBlock:^{
                    [self.navigationController popViewControllerAnimated:YES];
                }];
            });
            
        }
        
    } FailedHandler:^(NSError *error) {
        
        NSLog(@"%@",error.description);
        
    }];
    
}

-(void)createTableView
{
    self.tipsArray = @[@"账号",@"原密码",@"新密码",@"确认密码"].mutableCopy;
    self.tipsValueArray = @[@"请输入账号",@"请输入密码",@"请输入6~20位数字字母组合",@"请再次输入密码",].mutableCopy;
    

    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 10, ETScreenW, 55 * 4) style:(UITableViewStyleGrouped)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.scrollEnabled = NO;
    [self.view addSubview:self.tableView];
    self.tableView.separatorStyle = UITableViewCellSelectionStyleNone;
    [self.tableView registerClass:[ChangePwdTableViewCell class] forCellReuseIdentifier:@"cell"];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.tipsArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ChangePwdTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.titleLab.text = self.tipsArray[indexPath.row];
    cell.tipsValueTf.placeholder = self.tipsValueArray[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (indexPath.row == 0) {
        cell.tipsValueTf.keyboardType = UIKeyboardTypeNumberPad;
        if ([AppTools shareInstance].userInfo != nil) {
            cell.tipsValueTf.text = [[AppTools shareInstance].userInfo loginName];
        }
        cell.tipsValueTf.userInteractionEnabled = NO;
    }
    
    if (indexPath.row == 1) {
        cell.tipsValueTf.secureTextEntry = YES;
        //换行设置为完成
        cell.tipsValueTf.returnKeyType = UIReturnKeyDone;
        cell.tipsValueTf.delegate = self;
    }
    if (indexPath.row == 2) {
        cell.tipsValueTf.secureTextEntry = YES;
        //换行设置为完成
        cell.tipsValueTf.returnKeyType = UIReturnKeyDone;
        cell.tipsValueTf.delegate = self;
    }
    if (indexPath.row == 3) {
        cell.lineView.hidden = YES;
        cell.tipsValueTf.secureTextEntry = YES;
        //换行设置为完成
        cell.tipsValueTf.returnKeyType = UIReturnKeyDone;
        cell.tipsValueTf.delegate = self;
    }
    return cell;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return nil;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 55;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.01f;
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [super touchesBegan:touches withEvent:event];
    
    NSIndexPath *indexPath0 = [NSIndexPath indexPathForRow:0 inSection:0];
    ChangePwdTableViewCell *cell0 = (ChangePwdTableViewCell *)[self.tableView cellForRowAtIndexPath:indexPath0];
    
    NSIndexPath *indexPath1 = [NSIndexPath indexPathForRow:1 inSection:0];
    ChangePwdTableViewCell *cell1 = (ChangePwdTableViewCell *)[self.tableView cellForRowAtIndexPath:indexPath1];
    
    NSIndexPath *indexPath2 = [NSIndexPath indexPathForRow:2 inSection:0];
    ChangePwdTableViewCell *cell2 = (ChangePwdTableViewCell *)[self.tableView cellForRowAtIndexPath:indexPath2];
    
    NSIndexPath *indexPath3 = [NSIndexPath indexPathForRow:3 inSection:0];
    ChangePwdTableViewCell *cell3 = (ChangePwdTableViewCell *)[self.tableView cellForRowAtIndexPath:indexPath3];
    
    [cell0.tipsValueTf resignFirstResponder];
    [cell1.tipsValueTf resignFirstResponder];
    [cell2.tipsValueTf resignFirstResponder];
    [cell3.tipsValueTf resignFirstResponder];
    
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    
    [textField resignFirstResponder];//取消第一响应者
    
    return YES;
}


@end
